﻿using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using OmegaEngine;

namespace OmegaEngine
{
    public class GUIBackgroundButton : GUIActor
    {
        Texture2D texture { get; set; }
        Color color = Color.White;
        bool isMouseOver = false;
        GUIButtonHandler action = null;

        // Tons o' Constructors
        public GUIBackgroundButton(Texture2D Texture, GUIButtonHandler Action)
            : base(Texture.Width, Texture.Height)
        {
            SetupTexture(Texture, Action, Color.White);
        }

        public GUIBackgroundButton(Texture2D Texture, GUIButtonHandler Action, GameScreen Parent)
            : base(Texture.Width, Texture.Height, Parent)
        {
            SetupTexture(Texture, Action, Color.White);
        }

        public GUIBackgroundButton(Texture2D Texture, GUIButtonHandler Action, int PositionX, int PositionY)
            : base(Texture.Width, Texture.Height, PositionX, PositionY)
        {
            SetupTexture(Texture, Action, Color.White);
        }

        public GUIBackgroundButton(Texture2D Texture, GUIButtonHandler Action, int PositionX, int PositionY, GameScreen Parent)
            : base(Texture.Width, Texture.Height, PositionX, PositionY, Parent)
        {
            SetupTexture(Texture, Action, Color.White);
        }

        public GUIBackgroundButton(Texture2D Texture, GUIButtonHandler Action, int PositionX, int PositionY, Color Color)
            : base(Texture.Width, Texture.Height, PositionX, PositionY)
        {
            SetupTexture(Texture, Action, Color);
        }

        public GUIBackgroundButton(Texture2D Texture, GUIButtonHandler Action, int PositionX, int PositionY, Color Color, GameScreen Parent)
            : base(Texture.Width, Texture.Height, PositionX, PositionY, Parent)
        {
            SetupTexture(Texture, Action, Color);
        }

        public GUIBackgroundButton(Texture2D Texture, GUIButtonHandler Action, int PositionX, int PositionY, int Width, int Height, Color Color)
            : base(Width, Height, PositionX, PositionY)
        {
            SetupTexture(Texture, Action, Color);
        }

        public GUIBackgroundButton(Texture2D Texture, GUIButtonHandler Action, int PositionX, int PositionY, int Width, int Height, Color Color, GameScreen Parent)
            : base(Width, Height, PositionX, PositionY, Parent)
        {
            SetupTexture(Texture, Action, Color);
        }

        void SetupTexture(Texture2D Texture, GUIButtonHandler Action, Color Color)
        {
            this.texture = Texture;
            this.color = Color;
            this.action = Action;
        }

        // Simple method for changing the texture, callable anywhere
        public void UpdateTexture(Texture2D Texture)
        {
            this.texture = Texture;
        }

        public override void Update()
        {
            MouseDevice Mouse = Engine.Services.GetService<MouseDevice>();

            this.isMouseOver = this.Rectangle.Contains((int)Mouse.Position.X, (int)Mouse.Position.Y);

            if (this.isMouseOver)
            {
                this.color.A = 128;
            }
            else
            {
                this.color.A = 255;
            }

            if (this.isMouseOver && Mouse.WasButtonPressed(MouseButtons.Left))
            {
                //Parent.Disable();
                this.action();
            }
        }

        public override void Draw()
        {
            Engine.SpriteBatch.Draw(this.texture, this.Rectangle, this.color);
            base.Draw();
        }
    }
}
